﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emoney.UserTraining.Model;
using Emoney.UserTraining.DAL;
using System.Timers;

namespace Emoney.UserTraining.BLL
{
    public class Training_ClickCountBLL
    {
        static Timer timer = new Timer(60 * 1000);
        static Training_ClickCountBLL()
        {
            timer.Elapsed += new ElapsedEventHandler(TimerDo);
            timer.Start();
        }

        private static void TimerDo(object sender, ElapsedEventArgs e)
        {
            try
            {
                while (que.Count > 0)
                {
                    string sql = GetExecSql();
                    SqlHelper.ExecuteNonQuery(DBConn.Log, System.Data.CommandType.Text, sql, null);
                }
            }
            catch
            { }
        }

        private static string GetExecSql()
        {
            StringBuilder sb = new StringBuilder();
            lock(que)
            {
                for (int i = 0; i < que.Count; i++)
                {
                    var c = que.Dequeue();
                    if (c != null)
                    {
                        sb.AppendLine(" if(exists(select 1 from Training_ClickCount where uname='" + c.uname + "' and typename='" + c.typename + "' and trainid='" + c.trainid + "' and addtime='" + c.addtime.ToString("yyyy-MM-dd") + "'))");
                        sb.AppendLine("begin");
                        sb.AppendLine("update Training_ClickCount set [count]=ISNULL([count],0)+1 where uname='" + c.uname + "' and typename='" + c.typename + "' and trainid='" + c.trainid + "' and addtime='" + c.addtime.ToString("yyyy-MM-dd") + "'");
                        sb.AppendLine("end");
                        sb.AppendLine("else");
                        sb.AppendLine("begin");
                        sb.AppendLine("insert into Training_ClickCount ([uname],[typename],[trainid],[addtime],[area],[pid],[typeremark],[count]) values");
                        sb.AppendLine("('" + c.uname + "','" + c.typename + "','" + c.trainid + "','" + c.addtime.ToString("yyyy-MM-dd") + "','" + c.area + "','" + c.pid + "','" + c.typeremark + "',1)");
                        sb.AppendLine("end");
                    }
                }
            }
            return sb.ToString();
        }

        private static Queue<Training_ClickCount> que = new Queue<Training_ClickCount>();

        public static void AddClickCount(string username,string area,string pid,string trainid,string typename,string remark)
        {
            if (string.IsNullOrEmpty(trainid))
            {
                trainid = "0";
            }
            if (string.IsNullOrEmpty(username))
            {
                username = "0";
            }
            Training_ClickCount c = new Training_ClickCount();
            c.uname = username;
            c.area = area;
            c.pid = pid;
            c.trainid = trainid;
            c.typename = typename;
            c.typeremark = remark;
            c.addtime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            lock (que)
            {
                que.Enqueue(c);
            }
        }
    }
}
